package com.fingerchar.api.web;

import com.fingerchar.api.service.TtUsdtRechargeService;
import com.fingerchar.api.service.TtUsdtService;
import com.fingerchar.api.service.FcUserService;
import com.fingerchar.core.base.controller.BaseController;
import com.fingerchar.core.constant.SysConfConstant;
import com.fingerchar.core.util.ResponseUtil;
import com.fingerchar.db.domain.FcUser;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * @author tangzf
 * @date 2022/9/5
 */
@RestController
@RequestMapping(SysConfConstant.URL_PREFIX + "/usdt")
public class FcUsdtController extends BaseController {

    @Resource
    public FcUserService fcUserService;
    @Resource
    public TtUsdtRechargeService ttUsdtRechargeService;
    @Resource
    public TtUsdtService fcUsdtService;

    /**
     * usdt 充值
     *
     * @param hash
     * @param amount
     * @return
     */
    @PostMapping("/recharge")
    public Object recharge(String hash, BigInteger amount) {
        String userAddress = (String) request.getAttribute("userAddress");
        logger.info("recharge address:{}, hash:{}, amount:{}", userAddress, hash, amount);
        FcUser user = fcUserService.getUserByAddress(userAddress);
        if (null == user) {
            return ResponseUtil.fail(400, "用户不存在");
        }
        fcUsdtService.recharge(hash, userAddress, amount);
        return ResponseUtil.ok();
    }

    /**
     * 提现
     *
     * @param hash
     * @param amount
     * @return
     */
    @PostMapping("/withdraw")
    public Object withdraw(String hash, BigInteger amount) {
        String userAddress = (String) request.getAttribute("userAddress");
        logger.info("recharge address:{}, hash:{}, amount:{}", userAddress, hash, amount);
        FcUser user = fcUserService.getUserByAddress(userAddress);
        if (null == user) {
            return ResponseUtil.fail(400, "用户不存在");
        }
        fcUsdtService.withdraw(hash, userAddress, amount);
        return ResponseUtil.ok();
    }

}
